package com.gs.api.projects.shejiao.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.api.core.Result;
import com.gs.api.projects.shejiao.entity.TMemberUser;
import com.gs.api.projects.shejiao.entity.TTeamlevel;
import com.gs.api.projects.shejiao.service.ITMemberUserService;
import com.gs.api.projects.shejiao.service.ITTeamlevelService;
import com.gs.api.projects.weixin.entity.WxUser;
import com.gs.api.projects.weixin.service.WxUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2021-10-14
 */

@RestController
@RequestMapping("{appid}/teamlevel")
public class TTeamlevelController {

    @Autowired
    private ITTeamlevelService itTeamlevelService;

    @Autowired
    private ITMemberUserService itMemberUserService;

    @GetMapping("/save")
    public Result save(@PathVariable String appid,String shareCode,String openid,boolean newUser){

        if(!newUser){
            return Result.failure("该用户不是新用户");
        }
        if(StringUtils.isNotBlank(shareCode)){

            TMemberUser memberUser = itMemberUserService.getOne(
                    new QueryWrapper<TMemberUser>().eq("share_code", shareCode)
                    .eq("status",1));
            if(null == memberUser){
                //会员信息获取失败
                return Result.failure("该邀请码不存在");
            }
            // TODO 查看邀请码是否本人
            if(memberUser.getUserId().equals(openid)){
                //会员信息获取失败
                return Result.failure("不能使用自己的邀请码");
            }
            TTeamlevel tealeaves = new TTeamlevel();
            tealeaves.setCreateTime(new Date());
            tealeaves.setLevel(1);
            tealeaves.setUserId(openid);
            tealeaves.setParentId(memberUser.getUserId());
            itTeamlevelService.save(tealeaves);
            return Result.success();
        }
        return Result.failure("邀请码为空");

    }

    @GetMapping("/selectMyTeam")
    public Result selectMyTeam(@PathVariable String appid,String openid,Integer offset){
        List<Map<String, Object>> maps = itTeamlevelService.selectMyTeam(openid, offset);
        return Result.success(maps);

    }


}
